<?php
class sys
{
	function gotoUrl($url, $timeout=0)
	{
		echo '<script>setTimeout(function(){document.location.href="'.$url.'"}, '.$timeout.');</script>';
		exit;
	}
	
	function goBack()
	{
		sys::gotoUrl($_SERVER['HTTP_REFERER']);
	}
	
	function getSqlString()
	{
		$args = func_get_args();
		
		$sql_format = trim($args[0]);
		if (empty($sql_format))
			return false;
	        
		$params = array();
	        for ($i=1; $i<count($args); $i++)
	        {
			$params[] = (!is_object($args[$i]) && !is_array($args[$i])) ? mysql_escape_string($args[$i]) : '';
	        }
	        
	        $sql = vsprintf($sql_format."\n", $params);
	        
	        return $sql;
	}
	
	function sql()
	{
		$args = func_get_args();
		
		$sql_format = trim($args[0]);
		if (empty($sql_format))
			return false;
	        
		$params = array();
	        for ($i=1; $i<count($args); $i++)
	        {
			$params[] = (!is_object($args[$i]) && !is_array($args[$i])) ? mysql_escape_string($args[$i]) : '';
	        }

	        $sql = vsprintf($sql_format, $params);

		if ($_REQUEST['debug_sql'])
		{
			echo "<br/>";
			echo $sql_format."<br/>";
			print_r($params);
			echo "<br/>";
			echo "# ".$sql." #<br/>";
		}
		
		$mysql_conn = mysql_connect('localhost', SYS_DB_USER, SYS_DB_PASSWORD) or die('数据库连接错误!');
	
		mysql_select_db(SYS_DEFAULT_DATABASE) or die('Could not select database') or die('连接数据库出错!');

		mysql_query("SET NAMES 'UTF8'"); 
		//mysql_query("SET CHARACTER SET UTF8"); 
		//mysql_query("SET CHARACTER_SET_RESULTS=UTF8");

		$res = mysql_query($sql);
		$last_insert_id = mysql_insert_id();
		
		if (!$res)
			return false;
		
		if (strtolower(substr(trim($sql), 0, 6)) == 'select')
		{
			$retval = array();
			while($arr = mysql_fetch_array($res, MYSQL_ASSOC))
			{
				$retval[] = $arr;
			}
			mysql_free_result($res);
			return $retval;
		}
		else if (strtolower(substr(trim($sql), 0, 6)) == 'insert')
		{
			return $last_insert_id;
		}
	}

	function log()
	{
		$log = array();
		$log[] = date("Y-m-d H:i:s");
		foreach (func_get_args() as $s)
		{
			if (is_array($s))
			{
				$log[] = print_r($s, true);
			}
			else
			{
				$log[] = $s;
			}
		}
		
		return file_put_contents($_SERVER['DOCUMENT_ROOT'].'/log.txt', trim(implode("\t", $log))."\n", FILE_APPEND);
	}
	
	function getTimeString($the_time)
	{
		$now_time = date("Y-m-d H:i:s",time());
		$now_time = strtotime($now_time);
		$show_time = strtotime($the_time);
		$dur = $now_time - $show_time;
		if ($dur < 0) {
			return $the_time;
		} else if ($dur < 60) {
			return $dur.'秒前';
		} else if ($dur < 3600) {
			return floor($dur/60).'分钟前';
		} else if ($dur < 86400) {
			return floor($dur/3600).'小时前';
		} else if ($dur < 259200) {
			return floor($dur/86400).'天前';
		} else {
			return $the_time;
		}
	}

	function tryMakeDir($dir)
	{
		if (!is_dir($dir))
		{
			if (!mkdir($dir, 0755, true))
			{
				return false;
			}
		}
		
		return true;
	}

}
